const app = getApp()
const {
  fromUtils
} = require("../../utils/index")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    infoList: [],
    isAction: false,
    type: 'shopAddress'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.data.type = options.type
    // this.data.type = 'iphone'
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      that.checkoutType(data.type)
      that.setData({
        type: data.type
      })
    })

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  checkoutType(type) {
    switch (type) {
      case 'shopAddress': // 修改地址
        this.data.infoList = [{
          title: '所在地区',
          value: '广西省-柳州市-城中区',
          valueType: 'address'
        }, {
          title: '详细地址',
          value: '万达广场 3 楼 6061 号',
          valueType: 'input',
          attr: {
            type: 'text',
            placeholder: '请输入详细地址',
            maxlength: '200',
            checkType: 'checkDetail'
          }
        }]
        this.data.isAction = true
        break;
      case 'iphone': // 手机号
        this.data.infoList = [{
          title: '手机号',
          value: '139****9831'
        }, {
          title: '新手机号',
          value: '',
          valueType: 'input',
          attr: {
            type: 'number',
            placeholder: '请输入新手机号',
            maxlength: '11',
            checkType: 'checkIphone'
          }
        }, {
          titleType: 'input',
          value: '',
          attr: {
            type: 'number',
            placeholder: '请输入验证码',
            maxlength: '6',
            checkType: 'checkCode'
          },
          valueType: 'button',
          buttonAttr: {
            cont: '获取验证码',
            isButtonAct: false
          }
        }]
        break;
      case 'email': // 修改邮箱
        this.data.infoList = [{
          title: '邮箱',
          value: '88888@qq.com'
        }, {
          title: '新邮箱',
          value: '',
          valueType: 'input',
          attr: {
            type: 'text',
            placeholder: '请输入新邮箱',
            maxlength: '100',
            checkType: 'checkEmail'
          }
        }, {
          titleType: 'input',
          value: '',
          attr: {
            type: 'number',
            placeholder: '请输入验证码',
            maxlength: '6',
            checkType: 'checkCode'
          },
          valueType: 'button',
          buttonAttr: {
            cont: '获取验证码',
            isButtonAct: false
          }
        }]
        break;
    }
    this.setData({
      infoList: this.data.infoList,
      isAction: this.data.isAction
    })
  },
  onInputBlur(e) {
    const index = e.currentTarget.dataset.index
    const value = e.detail.value
    const msg = fromUtils[this.data.infoList[index].attr.checkType](value)
    if (msg) {
      app.showMsg(msg)
      this.data.infoList[index].value = ''
      this.data.isAction = false
    } else {
      this.data.infoList[index].value = value
      this.data.isAction = !this.data.infoList.find(i => i.value === '')
    }
    this.setData({
      infoList: this.data.infoList,
      isAction: this.data.isAction
    })
  },
  onSendCode() {
    if (this.data.infoList[1].value !== '') {
      console.log(this.data.infoList[1].value)
    } else {
      this.data.type == 'email' ? app.showMsg('请输入新邮箱') : app.showMsg('请输入新手机号')
    }
  },
  onSend() {
    if (!this.data.isAction) return
    const arr = this.data.infoList.filter(i => i.value !== '').map(s => s.value)
    console.log(arr)
  },
  onsubmit(e){
    wx.showToast({
      title: '提交成功',
      complete: () => {
        wx.navigateBack()
      }
    })
  }
})